package me.lc.linklinkchrist.layer;

import me.lc.linklinkchrist.R;
import me.lc.linklinkchrist.core.GameSystem;
import me.lc.linklinkchrist.core.StageManager;
import me.lc.linklinkchrist.util.AudioHelper;

import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.MoveTo;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.actions.Sequence;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYPoint;

/**
 * 主选单背景图层
 * 
 * @author Administrator
 * 
 */
public class MainMenuBGLayer extends Layer {

	Button audioOnButton;
	Button audioOffButton;

	public MainMenuBGLayer() {
		Texture2D tex = GameSystem.mainMenuBgTex;
		Sprite sp = Sprite.make(tex);
		sp.setAnchorPercent(0, 0);
		sp.setPosition(WYPoint.make(0, 0));
		sp.setScaleX(GameSystem.Screen_Width / tex.getWidth());
		sp.setScaleY(GameSystem.Screen_ActuallyHeight / tex.getHeight());
		addChild(sp);
		initAudioControl();
	}

	private void initAudioControl() {

		float x = GameSystem.Screen_Width * 0.05f;
		float y = GameSystem.Screen_ActuallyHeight * 0.04375f;
		audioOnButton = Button.make(R.drawable.audio_on, R.drawable.audio_on,
				this, "onAudioOnClick");
		audioOnButton.setAnchorPercent(0, 0);
		audioOnButton.setPosition(x, y);
		this.addChild(audioOnButton);
		audioOffButton = Button.make(R.drawable.audio_off,
				R.drawable.audio_off, this, "onAudioOffClick");
		audioOffButton.setAnchorPercent(0, 0);
		audioOffButton.setPosition(x, y);

		this.addChild(audioOffButton);

		if (StageManager.instance.isAudioOn == 1) {
			audioOffButton.setVisible(false);
			AudioHelper.playBackgoundMusic();
		} else {
			audioOnButton.setVisible(false);
		}
	}

	public void onAudioOnClick() {
		AudioHelper.stopBackgoundMusic();
		audioOffButton.setVisible(true);
		audioOnButton.setVisible(false);
		StageManager.instance.isAudioOn = 0;
		StageManager.instance.saveIsAudioOn();
	}

	public void onAudioOffClick() {
		AudioHelper.playBackgoundMusic();
		audioOffButton.setVisible(false);
		audioOnButton.setVisible(true);
		StageManager.instance.isAudioOn = 1;
		StageManager.instance.saveIsAudioOn();
	}
}
